package top.ijiujiu.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import top.ijiujiu.buried.MethodLog;
import top.ijiujiu.buried.MethodType;
import top.ijiujiu.entity.SysUser;
import top.ijiujiu.exception.DefaultException;
import top.ijiujiu.service.ISysUserService;
import top.ijiujiu.util.ResultUtil;

import java.util.List;

/**
 * 用户表 前端控制器
 *
 * @Author 神秘的黄瓜
 * @Version 1.0
 * @Date 2020-04-01
 */
@Api(value = "用户表接口API", tags = {"用户表接口API"})
@RestController
@RequestMapping("sysUser")
public class SysUserController {

    private final static Logger LOGGER = LoggerFactory.getLogger(SysUserController.class);


    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private PasswordEncoder passwordEncoder;


    /**
     * 根据ID查询数据
     *
     * @param id
     * @return
     * @throws DefaultException
     */
    @MethodLog(remark = "根据ID查询用户信息", methodType = MethodType.FIND)
    @ApiOperation(value = "根据ID查询数据", notes = "根据ID查询数据")
    @GetMapping("getOne/{id}")
    public ResultUtil<SysUser> getOne(@PathVariable("id") String id) throws DefaultException {
        if (StringUtils.isEmpty(id)){
            throw new DefaultException(HttpStatus.INTERNAL_SERVER_ERROR, "请求参数为空~~~~~~");
        }
        LOGGER.info("入参为:[{}]", id);
        ResultUtil<SysUser> data = new ResultUtil<>();
        return data.success(this.sysUserService.getById(id));
    }



    /**
     * 查询全部数据
     *
     * @return
     */
    @MethodLog(remark = "查询全部用户信息", methodType = MethodType.FIND)
    @ApiOperation(value = "查询全部数据", notes = "查询全部数据")
    @GetMapping("getAll")
    public ResultUtil<List<SysUser>> getAll() {
        ResultUtil<List<SysUser>> data = new ResultUtil<>();
        return data.success(this.sysUserService.list());
    }


    /**
     * 新增一条数据
     *
     * @param param
     * @return
     * @throws DefaultException
     */
    @MethodLog(remark = "新增一条用户信息", methodType = MethodType.ADD)
    @ApiOperation(value = "新增一条数据", notes = "新增一条数据")
    @PostMapping("add")
    public ResultUtil<Boolean> add(@RequestBody SysUser param) throws DefaultException {
        if (ObjectUtils.isEmpty(param)) {
            throw new DefaultException(HttpStatus.INTERNAL_SERVER_ERROR, "请求参数为空~~~~~~");
        }
        LOGGER.info("入参为:[{}]", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (!StringUtils.isEmpty(param.getPwd())) {
            param.setPwd(passwordEncoder.encode(param.getPwd()));
        }
        return data.success(this.sysUserService.save(param));
    }


    /**
     * 修改一条数据
     *
     * @param param
     * @return
     * @throws DefaultException
     */
    @MethodLog(remark = "修改一条用户信息", methodType = MethodType.UPDATE)
    @ApiOperation(value = "修改一条数据", notes = "修改一条数据")
    @PatchMapping("edit")
    public ResultUtil<Boolean> edit(@RequestBody SysUser param) throws DefaultException {
        if (ObjectUtils.isEmpty(param)) {
            throw new DefaultException(HttpStatus.INTERNAL_SERVER_ERROR, "请求参数为空~~~~~~");
        }
        LOGGER.info("入参为:[{}]", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        return data.success(this.sysUserService.updateById(param));
    }


    /**
     * 根据ID删除数据
     *
     * @param id
     * @return
     * @throws DefaultException
     */
    @MethodLog(remark = "根据ID删除一条用户信息", methodType = MethodType.DELETE)
    @ApiOperation(value = "根据ID删除数据", notes = "根据ID删除数据")
    @DeleteMapping("delete/{id}")
    public ResultUtil<Boolean> delete(@PathVariable("id") String id) throws DefaultException {
        if (StringUtils.isEmpty(id)){
            throw new DefaultException(HttpStatus.INTERNAL_SERVER_ERROR, "请求参数为空~~~~~~");
        }
        LOGGER.info("入参为:[{}]", id);
        ResultUtil<Boolean> data = new ResultUtil<>();
        return data.success(this.sysUserService.removeById(id));
    }


}

